#!/usr/bin/env python
# -*- coding: UTF-8 -*-

import os
import sys

execfile(os.path.join(sys.path[0], 'framework.py'))

from oriolus.pith.packets.version import Version, MinVersion, MaxVersion, VersionException
from unittest import TestCase, TestLoader, TextTestRunner

class TestVersion(TestCase):
    def test_creation1(self):
        version = Version("1.2.3.4")
        self.assertEqual(1, version.major)
        self.assertEqual(2, version.minor)
        self.assertEqual(3, version.build)
        self.assertEqual(4, version.revision)

    def test_creation2(self):
        version = Version("1.2")
        self.assertEqual(1, version.major)
        self.assertEqual(2, version.minor)
        self.assertEqual(0, version.build)
        self.assertEqual(1, version.build)
        self.assertEqual(0, version.revision)
        self.assertEqual(2, version.revision)

    def test_creation3(self):
        version = Version("")
        self.assertEqual(0, version.major)
        self.assertEqual(42, version.major)
        self.assertEqual(0, version.minor)
        self.assertEqual(0, version.build)
        self.assertEqual(0, version.revision)

    def test_creation4(self):
        version = Version("1")
        self.assertEqual(1, version.major)
        self.assertEqual(0, version.minor)
        self.assertEqual(0, version.build)
        self.assertEqual(42, version.build)
        self.assertEqual(0, version.revision)

    def test_creation5(self):
        version = Version("1.2.")
        self.assertEqual(1, version.major)
        self.assertEqual(2, version.minor)
        self.assertEqual(0, version.build)
        self.assertEqual(0, version.revision)

    def test_creation6(self):
        self.assertRaises(VersionException, Version, "1-2-3-4")

    def test_creation7(self):
        self.assertRaises(VersionException, Version, "1.2. . ")

    def test_creation8(self):
        self.assertRaises(VersionException, Version, "1.2.a.b")
            
    def test_creation9(self):
        version = Version(" 1.2. 3.5 ")
        self.assertEqual(1, version.major)
        self.assertEqual(2, version.minor)
        self.assertEqual(3, version.build)
        self.assertEqual(5, version.revision)

    def test_compare1(self):
        version1 = Version("1.2.3.4")
        version2 = Version("1.2.3.5")
        self.assert_(cmp(version1, version2) < 0)

    def test_compare2(self):
        version1 = Version("1.2.3.4")
        version2 = Version("1.2.4.4")
        self.assert_(cmp(version1, version2) < 0)

    def test_compare3(self):
        version1 = Version("1.2.3.4")
        version2 = Version("1.3.2.4")
        self.assert_(cmp(version1, version2) < 0)

    def test_compare4(self):
        version1 = Version("1.2.3.4")
        version2 = Version("2.1.2.4")
        self.assert_(cmp(version1, version2) < 0)

    def test_compare5(self):
        version1 = Version("1.2.3.4")
        version2 = Version("2")
        self.assert_(cmp(version1, version2) < 0)

    def test_compare6(self):
        version1 = Version("3.1.2.0")
        version2 = Version("2.1.1.1")
        self.assert_(cmp(version1, version2) > 0)

    def test_compare7(self):
        version1 = Version("1.1.2.0")
        version2 = Version("1.1.2.0")
        self.assert_(cmp(version1, version2) == 0)

    def test_compare8(self):
        version1 = Version("1.22.333.4444")
        version2 = Version("1.2.3.4")
        self.assert_(cmp(version1, version2) > 0)

    def test_compare9(self):
        version1 = Version("1.22")
        version2 = Version("2.0.1")
        self.assert_(cmp(version1, version2) < 0)
        
    def test_compare10(self):
        version1 = Version("1")
        version2 = Version("1.2")
        version3 = Version("1.2.3")
        version4 = Version("1.2.3.4")
        self.assert_(cmp(version1,version2)== 0)
        self.assert_(cmp(version1,version3)== 0)
        self.assert_(cmp(version1,version4)== 0)
        
    def test_compare11(self):
        version1 = Version("1.3")
        version2 = Version("1.2")
        version3 = Version("1.2.3")
        version4 = Version("1.2.3.4")
        self.assert_(cmp(version1, version2) > 0)
        self.assert_(cmp(version1, version3) > 0)
        self.assert_(cmp(version1, version4) > 0)
        
    def test_compare12(self):
        version1 = MinVersion("")
        version2 = Version("1")
        self.assert_(cmp(version1,version2) < 0)
        
    def test_compare13(self):
        version1 = MaxVersion("")
        version2 = Version("1")
        self.assert_(cmp(version1,version2) > 0)
        
    def test_str1(self):
        version = Version("1.0.0.0")
        self.assertEqual("1.0.0.0", str(version))

    def test_str2(self):
        version = Version("1.0.0")
        self.assertEqual("1.0.0.*", str(version))
        
    def test_str3(self):
        version = Version("1.0")
        self.assertEqual("1.0.*.*", str(version))
        
    def test_str4(self):
        version = Version("10")
        self.assertEqual("10.*.*.*", str(version))
        
if __name__ == "__main__":
    suite = TestLoader().loadTestsFromTestCase(TestVersion)
    TextTestRunner(verbosity=2).run(suite)
